
package com.bk.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;

import com.bk.base.BaseDaoImpl;
import com.bk.dao.OrderDao;
import com.bk.model.Order;
import com.bk.model.OrderItem;
import com.bk.utils.C3P0Util;

public class OrderDaoImpl extends BaseDaoImpl<Order> implements OrderDao{

	@Override
	public boolean createOrder(Order o) throws SQLException {
		//获得数据源
		DataSource dataSource = C3P0Util.getDataSource();
		QueryRunner qr=new QueryRunner(dataSource);
		//获得链接
		Connection conn = dataSource.getConnection();
		//开启事务
		conn.setAutoCommit(false);
		//订单表存储数据
		String sql="insert into orders values(?,?,?,?,?,?,?,?)";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		qr.update(conn,sql,new Object[]{o.getOid(),sdf.format(o.getOrdertime()),
				o.getTotal(),o.getState(),o.getAddress(),o.getName(),o.getTelephone(),o.getUser().getUid()});
		//订单项表存储数据
		String sql1="insert into orderitem values(?,?,?,?,?)";

		for(OrderItem it:o.getOrderitems()){
		qr.update(conn,sql1,new Object[]{it.getItemid(),it.getCount(),it.getSubtotal(),it.getProduct().getPid(),it.getOrder().getOid()});
			
			
		}
		conn.commit();
		return true;
			
	}

}

	